<template>
  <div class="app-container">
    <!-- 查询参数及按钮部分 -->
    <div class="button">
      <div class="filter-container">
        <el-button
          size="small"
          class="filter-item"
          type="primary"
          style="margin-left: 10px;"
          @click="handleAdd"
        >
          新增
        </el-button>
        <el-button
          size="small"
          class="filter-item"
          type="primary"
          @click="handleEdit"
        >
          修改
        </el-button>
        <el-button
          size="small"
          class="filter-item"
          type="primary"
          :loading="saveBtnLoading"
          @click="handleSave"
        >
          保存
        </el-button>
      </div>
    </div>
    <!-- 表格部分 -->
    <el-table
      ref="singalTable"
      v-loading="listLoading"
      :data="list"
      :stripe="true"
      :height="tableHeight"
      fit
      :header-cell-style="{background:'#eef1f6',color:'#606266'}"
    >
      <template slot="empty">
        <tableEmpty />
      </template>
      <el-table-column
        type="selection"
        align="center"
        width="55"
      />
      <el-table-column
        type="index"
        align="center"
        width="80"
        label="序号"
      />
      <el-table-column
        label="工资类型编码"
        align="center"
      >
        <template slot-scope="{row}">
          <div v-if="row.isAddOperate">
            <el-input
              v-model="row.code"
              maxlength="20"
              placeholder="请输入"
            />
          </div>
          <span v-else>{{ row.code }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="工资类型名称"
        align="center"
      >
        <template slot-scope="{row}">
          <div v-if="row.isOperate">
            <el-input
              v-model="row.name"
              maxlength="20"
              placeholder="请输入"
            />
          </div>
          <span v-else>{{ row.name }}</span>
        </template>
      </el-table-column>
        <el-table-column fixed="right" label="操作" width="100">
            <template slot-scope="scope">
              <el-button type="text" size="small" @click="handleDelete(scope.row)">删除</el-button>
            </template>
          </el-table-column>
    </el-table>
    <!--分页部分-->
    <pagination
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {
  queryPage,
  submitData,
  deleteSalaryType
} from '@/api/salaryConfigManage/salaryTypeBaseData'
import Pagination from '@/components/Pagination'
import tableEmpty from '@/components/table/empty'

export default {
  name: 'SalaryProjectMaintenance',
  components: {
    Pagination,
    tableEmpty
  },
  filters: {
    dictFilter(value, dictType, that) {
      const dict = that.dict[dictType]
      for (const item of dict) {
        if (value === item.value) {
          return item.label
        }
      }
    }
  },
  data() {
    return {
      tableHeight: null,
      that: this,
      submitIds: [],
      submitList: [],
      addItem: {
        id: '',
        code: '',
        name: '',
        isOperate: true,
        isAddOperate:true
      },
      list: [],
      total: 0,
      listLoading: true,
      removeBtnLoading: false,
      saveBtnLoading: false,
      listQuery: {
        page: 1,
        limit: 200
      }
    }
  },
  computed: {
    ids() {
      const selection = this.$refs.singalTable.selection
      const arr = []
      selection.forEach(item => {
        if (!item.isNewItem) {
          arr.push(item.id)
        }
      })
      return arr.join(',')
    }
  },
  watch: {
    list(val) {
      this.doLayout()
    }
  },
  created() {
    this.getList()
  },
  mounted() {
    this.getTbHeight()
  },
  beforeMount() {
    window.addEventListener('resize', this.getTbHeight)
  },

  methods: {
    /* 重新渲染table组件 */
    doLayout() {
      this.$nextTick(() => {
        this.$refs.singalTable.doLayout()
      })
    },
    // 获取table高度的方法
    getTbHeight() {
      this.tableHeight = window.innerHeight - 204
    },
    // 查询
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },

    getList() {
      this.listLoading = true
      queryPage(this.listQuery)
        .then(response => {
          this.list = response.data.rows
          this.total = response.data.total
          this.listLoading = false
          this.initData()
        })
        .catch(() => {
          this.initData()
        })
    },

    initData() {
      this.submitIds = []
      this.submitList = []
    },

    handleAdd() {
      // this.$confirm('工资项名称只能填写汉字或英文小括号！', '提示', {
      //   closeOnClickModal: false,
      //   confirmButtonText: '确定',
      //   cancelButtonText: '取消',
      //   type: 'warning'
      // }).then(() => {
      const addItem = { ...this.addItem }
      addItem.isNewItem = Math.random()
        .toString(16)
        .substr(2, 8)
      this.list.unshift(addItem)
      this.submitList.push(addItem)
      // })
    },

    handleEdit() {
      const selection = this.$refs.singalTable.selection
      if (selection.length !== 1) {
        this.$message({
          message: '请选择一条数据',
          type: 'warning',
          showClose: true,
          duration: 4000
        })
        return
      }
      if (!this.submitIds.includes(selection[0].id)) {
        this.$set(selection[0], 'isOperate', true)
        this.submitIds.push(selection[0].id)
        this.submitList.push(selection[0])
      }
    },

    removeNewItem(selection) {
      const newItemArr = []
      selection.forEach(item => {
        if (item.isNewItem) {
          newItemArr.push(item.isNewItem)
        }
      })
      if (newItemArr.length > 0) {
        const list = this.list
        for (let i = 0; i < list.length; i++) {
          if (newItemArr.indexOf(list[i].isNewItem) > -1) {
            list.splice(i--, 1)
          }
        }
        const submitList = this.submitList
        for (let i = 0; i < submitList.length; i++) {
          if (newItemArr.indexOf(submitList[i].isNewItem) > -1) {
            submitList.splice(i--, 1)
          }
        }
      }
    },

    handleSave() {
      const submitList = this.submitList
      if (submitList.length === 0) {
        this.$message({
          message: '没有可保存的数据',
          type: 'warning',
          showClose: true,
          duration: 2000
        })
        return
      }
      for (const item of submitList) {
        if (!item.name) {
          this.$message({
            message: '工资类型为必填项',
            type: 'warning',
            showClose: true,
            duration: 2000
          })
          return
        }
        
        if (!item.code) {
          this.$message({
            message: '工资类型编码为必填项',
            type: 'warning',
            showClose: true,
            duration: 2000
          })
          return
        }
        
      }
      this.saveBtnLoading = true
      
      submitData(submitList)
        .then(response => {
          this.$message({
            message: '保存成功',
            type: 'success',
            showClose: true,
            duration: 2000
          })
          this.handleFilter()
          this.saveBtnLoading = false
        })
        .catch(() => {
          this.saveBtnLoading = false
        })
    },
    //删除方法
      handleDelete(row) {
        if(row.id==''||row.code==''||row.name==''){
         const index = this.list.indexOf(row)
        this.list.splice(index, 1)
        }else{
           this.$confirm(row.name+' 删除, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
          center: true
        }).then(() => {
          deleteSalaryType(row).then(() => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
           this.getList()
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
        }
    },
  }
}
</script>

<style lang="scss" scoped>
.app-title {
  margin-bottom: 15px;
  height: 20px;
  border-bottom: 1px solid #dcdfe6;
}

.button {
  float: right;
}

/** 文本框高度 **/
::v-deep .el-input {
  width: auto !important;
}
</style>
